package cn.loveq.android.player;

import cn.loveq.android.R;
import cn.loveq.android.R.id;
import cn.loveq.android.R.layout;
import cn.loveq.android.R.string;

import com.spoledge.aacplayer.AACFileChunkPlayer;
import com.spoledge.aacplayer.AACPlayer;
import com.spoledge.aacplayer.ArrayAACPlayer;
import com.spoledge.aacplayer.ArrayDecoder;
import com.spoledge.aacplayer.Decoder;
import com.spoledge.aacplayer.PlayerCallback;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;

/**
 * 
 *    
 * 项目名称：loveq4Weibo   
 * 类名称：LoveQPlayer   
 * 类描述： LoveQ播放器  
 * 创建人：李伟豪  
 * 创建时间：2011-12-3 下午05:26:43   
 * 修改人：Administrator   
 * 修改时间：2011-12-3 下午05:26:43   
 * 修改备注：   
 * @version    
 *
 */
public class LoveQPlayer extends Activity implements View.OnClickListener, PlayerCallback {
	private AutoCompleteTextView urlView;
	private Button btnFaad2;
	private Button btnFFmpeg;
	private Button btnOpenCORE;
	private Button btnMMSWMA;
	private Button btnStop;
	private TextView txtStatus;
	private TextView txtPlayStatus;
	private EditText txtBufAudio;
	private EditText txtBufDecode;
	private ProgressBar progress;
	private Handler uiHandler;

	/**
	 * Decoder features: FAAD | FFmpeg | OpenCORE
	 */
	private int dfeatures;

	private AACPlayer aacPlayer;
	private AACFileChunkPlayer aacFileChunkPlayer;

	// //////////////////////////////////////////////////////////////////////////
	// PlayerCallback
	// //////////////////////////////////////////////////////////////////////////

	private boolean playerStarted;

	public void playerStarted() {
		uiHandler.post(new Runnable() {
			public void run() {
				txtBufAudio.setEnabled(false);
				txtBufDecode.setEnabled(false);
				btnFaad2.setEnabled(false);
				btnFFmpeg.setEnabled(false);
				btnOpenCORE.setEnabled(false);
				btnMMSWMA.setEnabled(false);
				btnStop.setEnabled(true);

				txtPlayStatus.setText(R.string.text_buffering);
				progress.setProgress(0);
				progress.setVisibility(View.VISIBLE);

				playerStarted = true;
			}
		});
	}

	/**
	 * This method is called periodically by PCMFeed.
	 * 
	 * @param isPlaying
	 *            false means that the PCM data are being buffered, but the audio is not playing yet
	 * 
	 * @param audioBufferSizeMs
	 *            the buffered audio data expressed in milliseconds of playing
	 * @param audioBufferCapacityMs
	 *            the total capacity of audio buffer expressed in milliseconds of playing
	 */
	public void playerPCMFeedBuffer(final boolean isPlaying, final int audioBufferSizeMs, final int audioBufferCapacityMs) {

		uiHandler.post(new Runnable() {
			public void run() {
				progress.setProgress(audioBufferSizeMs * progress.getMax() / audioBufferCapacityMs);
				if (isPlaying)
					txtPlayStatus.setText(R.string.text_playing);
			}
		});
	}

	public void playerStopped(final int perf) {
		uiHandler.post(new Runnable() {
			public void run() {
				enableButtons();
				btnStop.setEnabled(false);
				txtBufAudio.setEnabled(true);
				txtBufDecode.setEnabled(true);
				txtStatus.setText(R.string.text_stopped);
				txtPlayStatus.setText("" + perf + " %");
				progress.setVisibility(View.INVISIBLE);

				playerStarted = false;
			}
		});
	}

	public void playerException(final Throwable t) {
		uiHandler.post(new Runnable() {
			public void run() {
				new AlertDialog.Builder(LoveQPlayer.this).setTitle(R.string.text_exception).setMessage(t.toString()).setNeutralButton(R.string.button_close,
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {
								dialog.cancel();
							}
						}).show();

				txtStatus.setText(R.string.text_stopped);

				if (playerStarted)
					playerStopped(0);
			}
		});
	}

	// //////////////////////////////////////////////////////////////////////////
	// OnClickListener
	// //////////////////////////////////////////////////////////////////////////

	/**
	 * Called when a view has been clicked.
	 */
	public void onClick(View v) {
		try {
			switch (v.getId()) {

			case R.id.view_main_button_faad2:
				start(Decoder.DECODER_FAAD2);
				txtStatus.setText(R.string.text_using_FAAD2);
				break;

			case R.id.view_main_button_ffmpeg:
				start(Decoder.DECODER_FFMPEG);
				txtStatus.setText(R.string.text_using_FFmpeg);
				break;

			case R.id.view_main_button_opencore:
				start(Decoder.DECODER_OPENCORE);
				txtStatus.setText(R.string.text_using_OpenCORE);
				break;

			case R.id.view_main_button_mmswma:
				start(Decoder.DECODER_FFMPEG_WMA);
				txtStatus.setText(R.string.text_using_MMSWMA);
				break;

			/*
			 * case R.id.view_main_button_file: stop(); aacFileChunkPlayer = new AACFileChunkPlayer( getUrl(), 8000, 8000 ); aacFileChunkPlayer.start(); txtStatus.setText(
			 * R.string.text_using_file_chunks ); break;
			 */

			case R.id.view_main_button_stop:
				stop();
				break;
			}
		} catch (Exception e) {
			Log.e("AACPlayerActivity", "exc", e);
		}
	}

	// //////////////////////////////////////////////////////////////////////////
	// Protected
	// //////////////////////////////////////////////////////////////////////////

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.test);

		btnFaad2 = (Button) findViewById(R.id.view_main_button_faad2);
		btnFFmpeg = (Button) findViewById(R.id.view_main_button_ffmpeg);
		btnOpenCORE = (Button) findViewById(R.id.view_main_button_opencore);
		btnMMSWMA = (Button) findViewById(R.id.view_main_button_mmswma);
		// Button b3 = (Button) findViewById( R.id.view_main_button_file );
		btnStop = (Button) findViewById(R.id.view_main_button_stop);

		urlView = (AutoCompleteTextView) findViewById(R.id.view_main_edit_url);
		txtStatus = (TextView) findViewById(R.id.view_main_text_what);
		txtPlayStatus = (TextView) findViewById(R.id.view_main_text_status);
		txtBufAudio = (EditText) findViewById(R.id.view_main_text_bufaudio);
		txtBufDecode = (EditText) findViewById(R.id.view_main_text_bufdecode);

		progress = (ProgressBar) findViewById(R.id.view_main_progress);

		txtBufAudio.setText(String.valueOf(AACPlayer.DEFAULT_AUDIO_BUFFER_CAPACITY_MS));
		txtBufDecode.setText(String.valueOf(AACPlayer.DEFAULT_DECODE_BUFFER_CAPACITY_MS));

		btnFaad2.setOnClickListener(this);
		btnFFmpeg.setOnClickListener(this);
		btnOpenCORE.setOnClickListener(this);
		btnMMSWMA.setOnClickListener(this);
		// b3.setOnClickListener( this );
		btnStop.setOnClickListener(this);

		dfeatures = ArrayDecoder.getFeatures();

		enableButtons();
		//urlView.setText("mms://218.30.114.31/guangdong_zhujiang_fm974");
		urlView.setText("mms://live.rgd.com.cn/974");
		// urlView.setText("mms://live.rgd.com.cn/974");
		uiHandler = new Handler();
	}

	@Override
	protected void onPause() {
		super.onPause();
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		stop();
	}

	private void start(int decoder) {
		stop();
		aacPlayer = new ArrayAACPlayer(ArrayDecoder.create(decoder), this, getInt(txtBufAudio), getInt(txtBufDecode));
		aacPlayer.playAsync(getUrl());
	}

	private void stop() {
		if (aacFileChunkPlayer != null) {
			aacFileChunkPlayer.stop();
			aacFileChunkPlayer = null;
		}
		if (aacPlayer != null) {
			aacPlayer.stop();
			aacPlayer = null;
		}
	}

	private String getUrl() {
		String ret = urlView.getText().toString();
		return ret;
	}

	private void enableButtons() {
		if ((dfeatures & Decoder.DECODER_FAAD2) != 0)
			btnFaad2.setEnabled(true);
		if ((dfeatures & Decoder.DECODER_FFMPEG) != 0)
			btnFFmpeg.setEnabled(true);
		if ((dfeatures & Decoder.DECODER_OPENCORE) != 0)
			btnOpenCORE.setEnabled(true);
		if ((dfeatures & Decoder.DECODER_FFMPEG_WMA) != 0)
			btnMMSWMA.setEnabled(true);
	}

	private int getInt(EditText et) {
		return Integer.parseInt(et.getText().toString());
	}
}
